package io.rehuo.modules.app.utils;

import io.rehuo.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.common.TemplateParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author shiguang
 * @date 2022年09月12日 17:15
 */
@Slf4j
public class StrUtils {

    public static synchronized String getOrderNum(Long userId) {
        //时间（精确到毫秒）
        DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
        String localDate = LocalDateTime.now().format(ofPattern);
        //3位随机数
        String randomNumeric = RandomStringUtils.randomNumeric(3);
        //5位用户id
        int subStrLength = 5;
        String sUserId = userId.toString();
        int length = sUserId.length();
        String str;
        if (length >= subStrLength) {
            str = sUserId.substring(length - subStrLength, length);
        } else {
            str = String.format("%0" + subStrLength + "d", userId);
        }
        String orderNum = localDate + randomNumeric + str;
        log.info("订单号:{}", orderNum);
        return orderNum;
    }

    public static synchronized String getOrderNewNum(Long userId) {
        //时间（精确到毫秒）
        DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("MMdd");
        String localDate = LocalDateTime.now().format(ofPattern);
        //3位随机数
        String randomNumeric = RandomStringUtils.randomNumeric(3);
        //5位用户id
        int subStrLength = 3;
        String sUserId = userId.toString();
        int length = sUserId.length();
        String str;

        str = String.format("%0" + length + "d", userId);

        String orderNum = localDate + randomNumeric + str;
        log.info("订单号:{}", orderNum);
        return orderNum;
    }

    public static String replaceTemplate(String code, String smsTemplate) {
        Map<String, Object> params = new HashMap<>();
        params.put("code", code);
        ;
        ExpressionParser parser = new SpelExpressionParser();
        TemplateParserContext parserContext = new TemplateParserContext();
        return parser.parseExpression(smsTemplate, parserContext).getValue(params, String.class);
    }

    public static String checkPhone(String mobile) {
        String userPhone = "";
        if (StringUtils.isBlank(mobile)) {
            return userPhone;
        }
        if (mobile.startsWith("0") && !mobile.startsWith("084")) {
            userPhone = "84" + mobile.substring(1, mobile.length());
        } else if (mobile.startsWith("084")) {
            userPhone = mobile.substring(1, mobile.length());
        } else {
            userPhone = mobile;
        }
        return userPhone;
    }

    public static void main(String[] args) {
//        for(int i = 0;i<10;i++){
//            System.out.println(getOrderNum(Long.valueOf(i)));
//        }
        System.out.println(checkPhone("789512401"));
    }
}
